package top.fullj.remoting.codec;

import com.caucho.hessian.io.Hessian2Input;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/** decode from ByteBuf to Message with hessian
 *
 * @author bruce.wu
 * @since 2022/2/15 16:32
 */
public class Hassian2Decoder extends MessageToMessageDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        ByteBufInputStream is = new ByteBufInputStream(msg);
        Hessian2Input input = new Hessian2Input(is);
        out.add(input.readObject());
        input.close();
    }
}
